﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AzureGames.Data;
using AzureGames.Domain.Models;
using AzureGames.Domain.Contracts;
using AzureGames.Model.Statistics;


namespace AzureGames.Domain.Handlers
{
    /// <summary>
    /// used to return a model containing the details of a specified game definition in relation to the current player
    /// </summary>
    public class GetGameDetails
    {
        private readonly IGameDefinitionRepository _gameDefinitionRepository;
        private readonly IUserStatisticsRepository _userstatisticsRepository;
        private readonly IGameStatisticsRepository _gamestatisticsRepository;

        public GetGameDetails(IGameDefinitionRepository gameDefinitionRepository, IUserStatisticsRepository userstatisticsRepository, IGameStatisticsRepository gamestatisticsRepository)
        {
            _gameDefinitionRepository = gameDefinitionRepository;
            _gamestatisticsRepository = gamestatisticsRepository;
            _userstatisticsRepository = userstatisticsRepository;
        }

        public virtual GameDetails Execute(int gameDefinitionID, string playerID)
        {
            try
            {
                // basic game definition
                // statistics 
                //   number of games played in the last week total
                //   number of games played in the last week for player
                // top 10 players
                var gameDefinition = _gameDefinitionRepository.GetGameDefinition(gameDefinitionID);

                var gameStatistics = _gamestatisticsRepository.GetGameStatistics(gameDefinitionID, RANGE.ThisWeek);
                var userStatistics = _userstatisticsRepository.GetUserStatistics(gameDefinitionID, playerID, RANGE.ThisWeek);

                return new GameDetails(gameDefinition, gameStatistics, userStatistics);
            }
            catch (InvalidOperationException e)
            {
                throw new BusinessServicesException("Unable to retrieve game definition from database.", e);
            }
        }
    }
}
